package xyz.vmcserver.wdibbs.model.service.impl;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;
import xyz.vmcserver.wdibbs.model.dao.ArticleMapper;
import xyz.vmcserver.wdibbs.model.dao.UserGroupMapper;
import xyz.vmcserver.wdibbs.model.po.ArticleView;
import xyz.vmcserver.wdibbs.model.po.UserGroup;
import xyz.vmcserver.wdibbs.model.service.ArticleService;
import xyz.vmcserver.wdibbs.model.vo.Article;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service("ArticleService")
public class ArticleServiceImpl implements ArticleService {

    public SqlSession sqlSessionInit(){
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        return sqlSession;
    }
    @Override
    public void postArticle(Article article) {
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = build.openSession();
            ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
            mapper.insertArticle(article);
            sqlSession.commit();
            sqlSession.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<ArticleView> latestArticles(Integer page) {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = build.openSession();
            ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
            List<ArticleView> articleViews = mapper.selectLatestArticleView(page);
            return articleViews;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//        return null;
    }

    @Override
    public Integer queryArticleNumber() {
        SqlSession sqlSession = sqlSessionInit();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        return mapper.queryArticleNumber();
    }

    @Override
    public List<ArticleView> searchArticleByKeyAndValuePages(String value, Integer page,String sortType,String sortValue) {
        if(null==value){
            return null;
        }
        if(null==sortType){
            sortType = "ASC";
        }
        if(null==sortValue){
            sortValue = "score";
        }
        SqlSession sqlSession = sqlSessionInit();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        List<ArticleView> articleViews = mapper.searchArticleByKeyAndValuePages(value, (page-1)*20, sortType, sortValue);
        return articleViews;
    }

}
